# coding=utf-8


def BWTEncode(str):
    str = str + '#'
    size = len(str)
    bwtstr = ['0'] * size
    result = []
    result.append(str)
    for j in range(size):
        for i in range(size):
            if i == size - 1:
                bwtstr[0] = str[i]
            else:
                bwtstr[i + 1] = str[i]
        bstr = ''
        for i in range(len(bwtstr)):
            bstr += bwtstr[i]
        if j == size - 1:
            break
        else:
            result.append(bstr)
            str = bstr
    result = sorted(result)
    l = ''
    for i in range(len(result)):
        l += result[i][-1]
    return l
def handle(salt):
    encoding_dict = {"1":"0", "1j":"2", "-1j":"3", "-1":"1"}
    res= []
    dict_= {'0':-1,'1':1}
    for i in range(0,6,3):
        temp = complex(dict_[salt[i]], dict_[salt[i + 1]]) / complex(1, 1)
        if temp.real != 0:
            ans = dict_[salt[i + 2]]* 1j
        else:
            ans = dict_[salt[i + 2]]
        if temp.imag == 0 and temp.real == 1:
            temp1 = "1j"
        elif temp.imag == 0 and temp.real == -1:
            temp1 = "-1j"
        elif temp.imag == 1 and temp.real == 0:
            temp1 = "1"
        else:
            temp1 = "-1"
        if ans.imag == 0 and ans.real == 1:
            ans = "1j"
        elif ans.imag == 0 and ans.real == -1:
            ans = "-1j"
        elif ans.imag == 1 and ans.real == 0:
            ans = "1"
        else:
            ans = "-1"
        res.append(temp1)
        res.append(ans)


    return ''.join([encoding_dict[str(i)] for i in res])
def right(data):
    result = []
    result.append(data)
    i = 0
    while i < 3:
        temp = []
        temp.insert(0, data[3])
        temp.insert(1, data[2])
        temp.insert(2, data[0])
        temp.insert(3, data[1])
        result.append(''.join(temp))
        data = temp
        i = i + 1
    L = ''
    F = ''
    sorted_list = sorted(result)
    for eachstr in result:
        L = L + eachstr[-1]
        F = F + eachstr[0]
    return L
from random import choice
yuan = []
ss =[]
for m in range(200):
    q = ['0','1']
    number = ''.join([choice(q) for i in range(6)])
    yuan.append(number)
    da = right(handle(number))
    ss.append(da)
print(''.join(yuan))
import collections
import huffman
temp =''.join(ss)
print(temp)
aq =[]
for i in range(0,len(temp),4):
    ssss = temp[i:i+4]
    if ssss not in aq:
        print(ssss)
        aq.append(ssss)
print(len(aq))
dict_aq = dict(zip(aq ,[0 for i in aq]))
for i in range(0,len(temp),4):
    dict_aq[temp[i:i+4]] = dict_aq[temp[i:i+4]]+1
print(sorted(dict_aq.items(), key = lambda kv:(kv[1], kv[0])))
# deencoding_dict = {"0":1, "2":1j, "3":-1j, "1":-1}
# qwe = [deencoding_dict[i] for i in temp]
# print([deencoding_dict[i] for i in da])
# aq =[1j, 1, 1, 1j,1, (-0-1j), 1j, -1]
# for i in range(len(qwe)-1):
#     print(qwe[i]*qwe[i+1])
# print(len(qwe))
